﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

//  Todo: 
//      Rename form to QuarterlyOrderForm.
//      Replace Year textbox with combobox filled with activity year.
//      Sum product for year.
//      Sum orders for year.

namespace Northwind.NET.FE.Forms
{
    public partial class QuarterlyOrderForm : Form
    {
        public QuarterlyOrderForm()
        {
            InitializeComponent();
        }

        private void fillByYearToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.customerOrderYearTableAdapter.FillByYear(this.quarterlyOrdersFormDataSet.CustomerOrderYear, ((decimal)(System.Convert.ChangeType(orderYearToolStripTextBox.Text, typeof(decimal)))));
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        private void QuaterlyOrderForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'quarterlyOrdersFormDataSet.OrderProductQuarter' table. You can move, or remove it, as needed.
            this.orderProductQuarterTableAdapter.Fill(this.quarterlyOrdersFormDataSet.OrderProductQuarter);

            fillByDefaultYear();        
        }

        private void fillByDefaultYear()
        {
            orderYearToolStripTextBox.Text = "1997";
            this.customerOrderYearTableAdapter.FillByYear(this.quarterlyOrdersFormDataSet.CustomerOrderYear, ((decimal)(System.Convert.ChangeType(orderYearToolStripTextBox.Text, typeof(decimal)))));
        }

        private decimal columnValue(DataGridViewRow row, int colIndex)
        {
            decimal value = 0;
            if (row.Cells[colIndex].Value != null)
                value = (decimal)row.Cells[colIndex].Value;
            return value;
        }

        private void orderProductQuarterDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            const int Q1_Col_Index = 3;
            const int Q2_Col_Index = 4;
            const int Q3_Col_Index = 5;
            const int Q4_Col_Index = 6;
            const int Y_Col_Index = 7;

            decimal totalOrders = 0.0m;
            if (orderProductQuarterDataGridView.RowCount > 0)
            {
                foreach (DataGridViewRow row in orderProductQuarterDataGridView.Rows)
                {
                    decimal rowSum = 0.0m;
                    rowSum += columnValue(row, Q1_Col_Index);
                    rowSum += columnValue(row, Q2_Col_Index);
                    rowSum += columnValue(row, Q3_Col_Index);
                    rowSum += columnValue(row, Q4_Col_Index);

                    row.Cells[Y_Col_Index].Value = rowSum;
  
                    totalOrders += rowSum;
                }
            }

            orderYearTotalTextBox.Text = string.Format("{0:N2}",totalOrders);  
        }

    }
}
